/*
** shooting.c for zomzom in /home/goupil_r//GameInLab/zomzom/src/goupil_r
**
** Made by robin goupil
** Login   <goupil_r@epitech.net>
**
** Started on  Sat Nov 17 23:07:32 2012 robin goupil
** Last update Sun Nov 18 16:22:13 2012 robin goupil
*/

#include <math.h>
#include "include/struct.h"
#include "include/player.h"
#include "include/shooting.h"
#include "include/world.h"

void		kill(void *target)
{

}

void		do_damage(void *parent, t_biped *target, t_weapon *weapon)
{
  if (target->health <= 0)
    kill(target);
  else
    {
      printf("%d -- ", target->health);
      target->health -= 10;
      printf("%d\n", target->health);
      if (target->health <= 0)
	kill(target);
    }
}

void		shoot(t_world *world)
{
  t_vec		a;
  t_vec		b;
  double	x;
  double	y;
  t_zombie	*zombie;
  double	closest_dir;
  t_zombie	*closest_zombie;

  a = world->player->biped->pos;
  b = world->reticle->pos;
  zombie = world->zombie;
  closest_dir = -1;
  closest_zombie = NULL;
  while (zombie)
    {
      if (col_seg_circle(&a, &b, &(zombie->biped->pos), zombie->biped->sprite->size))
	{
	  x = zombie->biped->pos.x - world->player->biped->pos.x;
	  y = zombie->biped->pos.y - world->player->biped->pos.y;
	  if (closest_dir == -1 || sqrt(x * x + y * y) < closest_dir)
	    {
	      printf("x:%f -- y:%f\n", zombie->biped->pos.x, zombie->biped->pos.y);
	      closest_zombie = zombie;
	      closest_dir = sqrt(x * x + y * y);
	    }
	}
      zombie = zombie->next;
    }
  if (closest_zombie)
    do_damage(closest_zombie, closest_zombie->biped, world->player->current_weapon);
}
